﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Service;
using System.IO;
using System.Drawing;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public class ActivityController : IActivityController
    {
        private IActivityService activityService;
        private IActivityImageService activityImageService;
        private IActivityView view;
        public ActivityController(IActivityView view)
        {
            this.view = view;
            activityService = Factory.GetInstance<IActivityService>();
            activityImageService = Factory.GetInstance<IActivityImageService>();
        }

        /// <summary>
        /// activityPID为null则为第一次投票
        /// </summary>
        /// <param name="activityPID"></param>
        /// <param name="activityTitle"></param>
        /// <param name="activityContent"></param>
        /// <param name="activityStartTime"></param>
        /// <param name="activityEndTime"></param>
        /// <param name="activityIfShowUser"></param>
        /// <param name="activityIfReadAll"></param>
        /// <param name="activityIfMultiSubmit"></param>
        /// <param name="createdBy"></param>
        public void AddActivity(string activityPID,
            string activityTitle,
            string activityContent,
            DateTime activityStartTime,
            DateTime activityEndTime,
            string activityImage,
            int activityIfShowUser,
            int activityIfReadAll,
            int activityIfMultiSubmit)
        {
            //check is null
            if (string.IsNullOrWhiteSpace(activityTitle))
            {
                view.OnSubmitError("标题不为空");
                return;
            }
            //if (string.IsNullOrWhiteSpace(activityContent))
            //{
            //    view.OnSubmitError("内容不为空");
            //    return;
            //}
            //check length
            if (Encoding.Default.GetByteCount(activityTitle) > 100)
            {
                view.OnSubmitError("标题过长");
                return;
            }

            //check date
            if (activityStartTime > activityEndTime)
            {
                view.OnSubmitError("开始时间必须比结束时间小");
                return;
            }

                
            activityService.AddActivity(activityPID,
                activityTitle,
                activityContent,
                activityStartTime,
                activityEndTime,
                activityIfShowUser,
                activityIfReadAll,
                activityIfMultiSubmit,
                "admin",
                ReadFile(activityImage));
            view.AddActivityComplete("新增成功");
        }

        public void ModifyActivity(string activityID,
            string activityTitle,
            string activityContent,
            DateTime activityStartTime,
            DateTime activityEndTime,
            string activityImage,
            int activityIfShowUser, 
            int activityIfReadAll, 
            int activityIfMultiSubmit)
        {
            //check is null
            if (string.IsNullOrWhiteSpace(activityTitle))
            {
                view.OnSubmitError("标题不为空");
                return;
            }
            //if (string.IsNullOrWhiteSpace(activityContent))
            //{
            //    view.OnSubmitError("内容不为空");
            //    return;
            //}
            //check length
            if (Encoding.Default.GetByteCount(activityTitle) > 100)
            {
                view.OnSubmitError("标题过长");
                return;
            }

            //check date
            if (activityStartTime > activityEndTime)
            {
                view.OnSubmitError("开始时间必须比结束时间小");
                return;
            }

            activityService.ModifyActivity(activityID,
             activityTitle,
             activityContent,
             activityStartTime,
             activityEndTime,
             activityIfShowUser,
             activityIfReadAll,
             activityIfMultiSubmit,
             "admin");

            byte[] img = ReadFile(activityImage);
            if (img!=null)
            {
                activityImageService.ModifyActivityImage(activityID, img);
            }

            view.ModifyActivityComplete("修改成功");
        }

        public void GetActivity(string activityID)
        {
            view.GetActivityComplete(activityService.GetActivityByIdWhenAdmin(activityID));
        }

        private byte[] ReadFile(string fileName)
        {
            bool isBroken = false;
            try
            {
                Bitmap bmp = new Bitmap(fileName);
                if (bmp == null)
                {
                    //图片损坏
                    isBroken = true;
                }
            }
            catch //(Exception ex)
            {
                //图片损坏
                isBroken = true;
            }

            if (isBroken)
                return null;

            FileStream pFileStream = null;
            byte[] pReadByte = null;

            try
            {
                pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
        }
    }
}
